# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("tidyverse","coefplot", "estimatr"))

library(tidyverse)
library(coefplot)
library(estimatr)

study_1 <- read_csv("study_1.csv")
study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

fit_0_b <- lm_robust(bush_general ~ Z_ad, data = filter(study_1, bilingual == 1))
fit_1_b <- lm_robust(vela_general ~ Z_vela, data = filter(study_2, bilingual == 1))
fit_1_m <- lm_robust(vela_general ~ Z_vela, data = filter(study_3, bilingual == 0))
fit_2_b <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_2, bilingual == 1))
fit_2_m <- lm_robust(coffman_general ~ Z_coffman, data = filter(study_3, bilingual == 0))

fit_3_b <- lm_robust(like_vela ~ Z_vela, data = filter(study_2, bilingual == 1))
fit_3_m <- lm_robust(like_vela ~ Z_vela, data = filter(study_3, bilingual == 0))
fit_4_b <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1))
fit_4_m <- lm_robust(like_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0))
fit_5_b <- lm_robust(like_bush ~ Z_ad, data = filter(study_1, bilingual == 1))

fit_6_b <- lm_robust(vela_cares ~ Z_vela, data = filter(study_2, bilingual == 1))
fit_6_m <- lm_robust(vela_cares ~ Z_vela, data = filter(study_3, bilingual == 0))
fit_7_b <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_2, bilingual == 1))
fit_7_m <- lm_robust(coffman_cares ~ Z_coffman, data = filter(study_3, bilingual == 0))
fit_8_b <- lm_robust(bush_cares ~ Z_ad, data = filter(study_1, bilingual == 1))

fit_9_b <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_2, bilingual == 1))
fit_9_m <- lm_robust(conf_in_vela ~ Z_vela, data = filter(study_3, bilingual == 0))
fit_10_b <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_2, bilingual == 1))
fit_10_m <- lm_robust(conf_in_coffman ~ Z_coffman, data = filter(study_3, bilingual == 0))
fit_11_b <- lm_robust(conf_in_bush ~ Z_ad, data = filter(study_1, bilingual == 1))

fit_list_b <- list(fit_0_b, fit_1_b, fit_2_b, fit_3_b, fit_4_b, fit_5_b, fit_6_b, fit_7_b, fit_8_b, fit_9_b, fit_10_b, fit_11_b)
fit_list_m <- list(fit_1_m, fit_2_m, fit_3_m, fit_4_m, fit_6_m, fit_7_m, fit_9_m, fit_10_m)

gg_df <- 
  bind_rows(
    (fit_list_b %>% map_df(tidy) %>% mutate(sample = "Bilingual Sample")),
    (fit_list_m %>% map_df(tidy) %>% mutate(sample = "Monolingual Sample"))) %>%
  filter(term != "(Intercept)")

gg_df <- 
  within(gg_df,{
    dv_group = rep(NA, nrow(gg_df))
    dv_group[outcome %in% c("like_coffman", "like_vela", "like_bush")] <- "Like Candidate \n (1-7)"
    dv_group[outcome %in% c("conf_in_coffman", "conf_in_vela", "conf_in_bush")] <- "Confidence Re: Immigration \n (1-4)"
    dv_group[outcome %in% c("coffman_general", "vela_general", "bush_general")] <- "Support Candidate in General \n (0-1)"
    dv_group[outcome %in% c("coffman_cares", "vela_cares", "bush_cares")] <- "Candidate Cares \n (0-1)"
    dv_group <- factor(dv_group, levels = c("Support Candidate in General \n (0-1)", "Like Candidate \n (1-7)", "Candidate Cares \n (0-1)", "Confidence Re: Immigration \n (1-4)"))
    dv_group <- factor(dv_group, levels = rev(levels(dv_group)))
    
    candidate = rep(NA, nrow(gg_df))
    candidate[term == "Z_coffman"] <- "White Republican \n Cong. Candidate (Coffman)"
    candidate[term == "Z_vela"] <- "Latino Democratic \n Cong. Candidate (Vela)"
    candidate[term == "Z_adspanish_ad"] <- "White Republican \n Pres. Candidate (Bush)"
    candidate <- factor(candidate, levels = c("White Republican \n Pres. Candidate (Bush)",
                                              "Latino Democratic \n Cong. Candidate (Vela)",
                                              "White Republican \n Cong. Candidate (Coffman)"))
  })


g <- 
ggplot(gg_df, aes(x = estimate, y = dv_group, group = sample, color = sample, shape = sample)) +
  geom_point(size = 2, position = position_dodgev(height = -.5)) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = -.5), height = 0) +
  facet_grid(~candidate) +
  scale_color_manual(values = c("red", "blue")) +
  geom_vline(xintercept = 0, linetype = "dashed") + 
  theme_bw() +
  xlab("Average Treatment Effect Estimates with 95% Confidence Intervals") +
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        axis.title = element_blank(),
        strip.background = element_blank())

ggsave(filename = "flores_coppock_figure_1.pdf", g, width = 7, height = 5)
  
